<?php
/**
 * AJAX Handler for Twitter: List - Lists the twitter accounts associated with the project.
 * NOTE: Also Returns information relative to if there is a twitter account being displayed, and which account that is.
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die('');
    }
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true) {
		$hasDisplayTwitter = $Project->hasTwitter();
		$twitterAccounts = $Project->getTwitters();
		$displayTwitter = $Project->getTwitterToDisplay();
		$js_i = 0;
		$js_names_index = '';
		
		foreach($twitterAccounts as $key => $value) {
		    $js_names_index .= "names[{$js_i}] = '$value';\n";
		    
		    $js_i++;
		}
		
		if($hasDisplayTwitter === true) {
		    $js_hasActive = 'var hasActive = true;';
		    $js_name_active = "var active_name = '{$displayTwitter}';";
		    $js_names_index .= "names[{$js_i}] = '{$displayTwitter}';\n";
		    $js_i++;
		} else {
		    $js_hasActive = 'var hasActive = false;';
		    $js_name_active = "var active_name = false;";
		}
		echo <<<EOHTML

		    <!DOCTYPE html>
		    <html>
			<head><title></title></head>
			<body>
			<script type="text/javascript">
			    var names = new Array();
			    {$js_names_index}
			    {$js_hasActive}
			    {$js_name_active}
				parent.TwitterEDJX.update(names, hasActive, active_name);
			</script>
			    
			</body>
		    </html>
		
EOHTML;
	    } else {
		throw new InvalidProjectException();
	    }
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die('');
	}
    } else {
	header("HTTP/1.1 400 Bad Request");
	die('');
    }
?>
    